package de.school.cbs.cube;
import java.awt.event.*;
import java.awt.AWTEvent;
import java.util.*;
import java.awt.Color;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
 
public class Led extends Sphere implements Observer {
    private int x, y, z;
    private TransformGroup group;
    private boolean enabled;
    private Appearance ap;
    private Color3f colorEnable;
    private Color3f colorDisable;

    public Led(int x, int y, int z, float size, TransformGroup group, Color cubeColor) {
        super(size);
        this.enabled = false;
        this.group = group;
        this.setPos(x, y, z);
        this.colorEnable  = new Color3f();
        this.colorDisable = new Color3f();
        this.colorEnable.set(cubeColor);
        this.colorDisable.set(cubeColor.darker().darker().darker());

        this.ap = new Appearance();
        this.ap.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_READ);
        this.ap.setCapability(Appearance.ALLOW_COLORING_ATTRIBUTES_WRITE);
        this.setAppearance(ap);

        this.setColor();
        this.display();
    }

    public void update(Observable o, Object e) {
        this.update(o, (AWTEvent) e);
    }
    public void update(Observable o, AWTEvent e) {
        if (e instanceof ActionEvent && ((ActionEvent)e).getActionCommand() == "color") {
            this.colorEnable.set((Color)e.getSource());
            this.colorDisable.set(((Color)e.getSource()).darker().darker().darker());
            this.setColor();
        } else if (e instanceof TextEvent) {
            int[] s = (int[])((TextEvent)e).getSource();
            if (
                    (
                     s[1] >= 0 && s[2] >= 0 && s[3] >= 0 &&
                     this.getY() == s[2] && this.getX() == s[1] && this.getZ() == s[3]
                    ) ||
                    (
                        s[2] >= 0 && s[1] == -1 && s[3] == -1 &&
                        this.getY() == s[2]
                    )
               ) {
                if (s[0] == 1) {
                    this.enable();
                } else {
                    this.disable();
                }    
            } 
            
        }
    }
    
    public void setPos(int x, int y, int z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public int getX() {
        return this.x;
    }

    public int getY() {
        return this.y;
    }

    public int getZ() {
        return this.z;
    }

    public void setColor() {
        ColoringAttributes ca;
        if (this.enabled) {
            ca = new ColoringAttributes(this.colorEnable, ColoringAttributes.NICEST);
        } else {
            ca = new ColoringAttributes(this.colorDisable, ColoringAttributes.NICEST);
        }
        
        this.ap.setColoringAttributes(ca);
    }

    public void enable() {
        this.enabled = true;
        this.setColor();
    }

    public void disable() {
        this.enabled = false;
        this.setColor();
    }

    public void display() {
        TransformGroup tg = new TransformGroup();
        Transform3D transform = new Transform3D();
        Vector3f vector = new Vector3f(this.x*0.5f, this.y*0.5f, this.z*0.5f);
        transform.setTranslation(vector);
        tg.setTransform(transform);
        tg.addChild(this);
        this.group.addChild(tg);
    }
}
 
